package one;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;

import java.lang.reflect.Type;

public class CustomJsonDeserializer implements JsonDeserializer<Boolean> {
    @Override
    public Boolean deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
        return jsonElement.getAsInt() == 0 ? false : true;
    }

    public static void main(String[] args) {
        String open = "{\"name\":\"open\",\"closed\":0}";
        String close = "{\"name\":\"close\",\"closed\":1}";

        Gson gson = new GsonBuilder().registerTypeAdapter(Boolean.class, new CustomJsonDeserializer())
                .create();

        Door openDoor = gson.fromJson(open, Door.class);
        System.out.println(openDoor);

        Door closeDoor = gson.fromJson(close, Door.class);
        System.out.println(closeDoor);

    }
}
